﻿package
{
	import com.greensock.TweenLite;
	
	import core.Config;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	/**
	 * 筹码列表
	 * 
	 * 注意选中的处理,以及代码的优化
	 * 
	 * 并且尽量代码要简洁,只有简洁的做对和能更容易理解,才不容易出问题
	 * 
	 * 一般情况下,如果写一个很简单的功能,一般每个方法在10行左右,如果超过10行或者是有20多行,就要想想是不是代码有问题了
	 * 
	 * TODO 效果需要重新整理
	 */ 
	
	public class ChipList extends MovieClip
	{
		public var leftBtn:SimpleButton;
		
		public var rightBtn:SimpleButton;
		
		private var container:Sprite;
		
		private var _mMask:Sprite;
		
		private var coreList:Object;//生成的筹码数组
		
		private var headIndex:int;
		private var tailIndex:int;
		private var numCore:int;
		
		private var _selected:ChipEffect;// 选中的筹码
		
		private var head:ChipEffect;// 头部
		
		private var tail:ChipEffect;// 尾部
		
		private var count:int;// countainer容器的总数
		
		private var isRuning:Boolean;
		
		private var clearID:uint;
		
		private var chipList:Array;
		
		public function ChipList()
		{
			super();
			chipList = [];
			for(var i:int = 0; i < Config.CHIP_LIST.length; i++){
				var item:Object = Config.CHIP_LIST[i];
				if(!item.hasOwnProperty("visible")){
					chipList.push(item);
				}
			}
			this.calculateTweenPosition();
			this.createChildren();
			this.configEvent();
			
		}
		
		private var list:Array = [];// 存放7个位置的数据
		
		private static const CIR_RADIUS:Number = 890;
		private static const CIR_FACT:Number = 15.533430;
		private static const CIR_FACT_OFF:Number = 1742.850884;
		private static const CIR_X:Number = 760;
		private static const CIR_Y:Number = -800;
		
		private static const W:Number = 85;
		private static const X_OFFSET:Number = -250;
		
		/**
		 * 计算出具体的位置
		 * 一共是7个位置
		 */		
		private function calculateTweenPosition():void {
			for(var i:int = -1; i < 6; i++){
				var angle:Number = (-(i * W) + CIR_FACT_OFF) / CIR_FACT * 0.017453;// π/180
				var position:Point = new Point();
				position.x = CIR_X + Math.cos(angle) * CIR_RADIUS + X_OFFSET;
				position.y = CIR_Y + Math.sin(angle) * CIR_RADIUS;
				this.list.push(position);
			}
		}
		
		
		/**
		 * 按钮中移动的控制，当移动完成之后才移除指定的筹码并且激活isRuning状态来激活鼠标
		 * */
		private function onComplete():void 
		{
			clearTimeout(clearID);
			container.removeChildAt(removedIndex);
			this.isRuning = false;
			
		}
		
		/**
		 * 创建筹码以及现实区域并且指定默认的选中的筹码
		 * */
		protected function createChildren():void {
			container = new Sprite();
			this.addChild(container);
			this.container.scaleX = this.container.scaleY = 0.5;
			var xx:Number = 0;
			var start:int = 0;//从第二个开始生成
			var row:int = 5;//第7个结束
			var end:int = start + row;
			for(var i:int = start; i < end; i++){
				var chipEffect:ChipEffect = new ChipEffect();
				chipEffect.data = chipList[i];
				var position:Point = this.list[i+1];
				chipEffect.x = position.x;
				chipEffect.y = position.y;
				container.addChild(chipEffect);// 需要排列位置
			}
			this.head = ChipEffect(container.getChildAt(0));
			this.tail = ChipEffect(container.getChildAt(4));
			this.headIndex = start;
			this.tailIndex = end - 1;
			this._selected = this.head;
			this._selected.selected = true;
		}
		
	
		private function configEvent():void {
			leftBtn.addEventListener(MouseEvent.CLICK,moveToLeft);
			rightBtn.addEventListener(MouseEvent.CLICK,moveToRight);
			this.addEventListener(MouseEvent.CLICK,checkChipHandler);
		}
		
		
		/**
		 * 获取筹码列表中选中的筹码
		 * */
		private function checkChipHandler(event:MouseEvent):void {
			var target:ChipEffect = event.target.parent as ChipEffect;
			if(target){
				if(this._selected == target)//如果选中筹码已经选中则跳出
				{
					return;
				}
				if(this._selected != null){
					this._selected.selected = false;
				}
				this._selected = target;
				this._selected.selected = true;
			}
		}
		
		
		
		/**
		 * 点击左部按纽时,最右边的一个ChipEffect会慢慢消失,左边的会慢慢显示使用使用EnterFrame就可以了
		 */ 
		
		private var removedIndex:int = -1;
		
		private function moveToLeft(event:MouseEvent):void
		{
			if(!isRuning)
			{
				this.isRuning = true;
				//---------------计算筹码的头部跟尾部----------------
				this.headIndex -= 1;
				this.tailIndex -= 1;
				this.calculateIndex();
	
				//-----------在筹码前面添加一个筹码-----------------
				var chipEffect:ChipEffect = new ChipEffect();
				chipEffect.data = chipList[this.headIndex];
				chipEffect.alpha = 0;
				container.addChildAt(chipEffect,0);// 必须要加到最前面
				//----------------------------
				this.checkSelected(chipEffect);
				this.removedIndex = container.numChildren - 1;
				var position:Point = this.list[0];
				chipEffect.x = position.x;
				chipEffect.y = position.y;// 取最前面的位置
				for(var i:int = 0; i < this.container.numChildren; i++){
					var target:ChipEffect = this.container.getChildAt(i) as ChipEffect;
					var p:Point = this.list[i + 1];
					if(i == 0){
						TweenLite.to(target,0.4,{x:p.x,y:p.y,alpha:1});
					}else if(i == this.container.numChildren - 1){
						TweenLite.to(target,0.4,{x:p.x,y:p.y,alpha:0});
					} else {
						TweenLite.to(target,0.4,{x:p.x,y:p.y});
					}
				}
				this.checkCompleted();
			}
		}
		
		
		/**
		 * 和左部按纽一样,只不过是效果相反
		 */ 
		private function moveToRight(event:MouseEvent):void 
		{
			if(!isRuning)
			{
				this.isRuning = true;
				this.headIndex += 1;
				this.tailIndex += 1;
				this.calculateIndex();
				
				//------在筹码前面添加一个筹码-------
				var chipEffect:ChipEffect = new ChipEffect();
				chipEffect.data = chipList[this.tailIndex];
				//chipEffect.x = container.getChildAt(container.numChildren-1).x + chipEffect.width;
				chipEffect.alpha = 0;
				container.addChild(chipEffect);
				//----------------------------
				this.checkSelected(chipEffect);
				// 全部向左移动
				//var toX:Number = container.x - chipEffect.width;
				this.removedIndex = 0;
				var position:Point = this.list[this.list.length - 1];
				chipEffect.x = position.x;
				chipEffect.y = position.y;// 取最后一个位置
				for(var i:int = 0; i < this.container.numChildren; i++){
					var target:ChipEffect = this.container.getChildAt(i) as ChipEffect;
					var p:Point = this.list[i];
					if(i == 0){
						TweenLite.to(target,0.4,{x:p.x,y:p.y,alpha:0});
					}else if(i == this.container.numChildren - 1){
						TweenLite.to(target,0.4,{x:p.x,y:p.y,alpha:1});
					} else {
						TweenLite.to(target,0.4,{x:p.x,y:p.y});
					}
				}
				this.checkCompleted();
			}
		}
		
		private function checkCompleted():void {
			clearID = setTimeout(this.onComplete,500);
		}
		
		/**
		 * 控制筹码飞出去的时候坐标
		 * */
		public function getSelectedPosition():Point
		{
			return this.container.localToGlobal(new Point(this._selected.x,this._selected.y));
		}
		
		/**
		 * 传入筹码的信息判断是否为选中筹码
		 * */
		private function checkSelected(value:ChipEffect):void {
			if(this.selected.data.value == value.data.value){
				this._selected = value;
				this.selected.selected = true;
			}
		}
		
		
		/**
		 * 计算头部跟尾部的筹码数字防止超出指定范围
		 * */
		
		private function calculateIndex():void {
			var count:int = chipList.length - 1;
			if(this.headIndex < 0){
				this.headIndex = count;
			}else if(this.headIndex > count){
				this.headIndex = 0;
			}
			if(this.tailIndex < 0){
				this.tailIndex = count;
			}else if(this.tailIndex > count){
				this.tailIndex = 0;
			}
		}
		
		/**
		 * 返回选中筹码object 
		 * */
		public function get selected():ChipEffect {
			return this._selected;
		}
		
		override public function set enabled(value:Boolean):void {
			super.enabled = value;
			this.leftBtn.visible = value;
			this.rightBtn.visible = value;
			this.mouseEnabled = value;
			this.mouseChildren = value;
		}
		
	}
}